(function (jq) {
    var requestURL;
    function init() {
            $.ajax({
                url: requestURL,
                type: 'GET',
                dataType: 'json',
                success: function (arg) {
                    if(arg.status){
                        createTableHead(arg.data.table_config);
                        createTableBody(arg.data.table_config, arg.data.data_list);
                    }else {
                        alert(arg.message);
                    }
                }
            })
        }
    function createTableHead(arg) {
            /*
            dataList = [{'hostname': 'c1.com', 'port': 11}, {'hostname': 'c2.com', 'port': 23}]
            tableConfig = [
				{
					'q': 'hostname',
					'title':'主机名',
					'display':1,
				},
				{
					'q': 'port',
					'title':'端口',
					'display':1,
				}
			]
             */
            var tr = document.createElement('tr')
            $.each(arg,function (k, v) {
                if(v.display){
                    var th = document.createElement('th');
                    th.innerText = v.title;
                    tr.append(th);
                }
            });
            $('#thead').append(tr)
        }

    function createTableBody(tableConfig, dataList) {
            $.each(dataList, function (k, row) {
                var tr = document.createElement('tr');
                $.each(tableConfig, function(k2,row2){
                    var td = document.createElement('td');
                    td.innerText=row[row2.q];
                    $(tr).append(td);
                });
                $('#tbody').append(tr);
            })
        }
    jq.extend({
       'ex': function (URL) {
           requestURL = URL;
           init();
       }
   })
})(jQuery);